package com.flyqiu.common.spring;

import com.flyqiu.common.bean.FlyQiuBeanManage;
import com.flyqiu.common.transaction.FlyQiuTransactionManage;
import com.flyqiu.common.transaction.SimpleTransactionManage;
import lombok.Getter;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Getter
@Configuration
@ComponentScan("com.flyqiu.common.spring")
public class FlyQiuCommonAutoConfig {
    private final AutowireCapableBeanFactory autowireCapableBeanFactory;

    /**
     * 上下文对象实例
     */
    private final ApplicationContext applicationContext;

    public FlyQiuCommonAutoConfig(AutowireCapableBeanFactory autowireCapableBeanFactory, ApplicationContext applicationContext) {
        this.autowireCapableBeanFactory = autowireCapableBeanFactory;
        this.applicationContext = applicationContext;
        FlyQiuSpringContextTools.setApplicationContext(applicationContext, autowireCapableBeanFactory);
    }

    @Bean
    @ConditionalOnMissingBean(FlyQiuTransactionManage.class)
    public FlyQiuTransactionManage flyQiuTransactionManage(PlatformTransactionManager platformTransactionManager) {
        return new SimpleTransactionManage(platformTransactionManager);
    }

    @Bean
    @ConditionalOnMissingBean(FlyQiuBeanManage.class)
    public FlyQiuBeanManage springProcBeanManage(ApplicationContext applicationContext, AutowireCapableBeanFactory capableBeanFactory) {
        return new FlyQiuSpringProcBeanManage(applicationContext, capableBeanFactory);
    }

}
